package com.yf.zzmusic.controller;


import com.yf.zzmusic.common.Constants;
import com.yf.zzmusic.common.PageInfo;
import com.yf.zzmusic.common.Result;
import com.yf.zzmusic.pojo.dto.musicCollect.MusicCollectDetailsDTO;
import com.yf.zzmusic.pojo.dto.musicCollect.MusicCollectPageDTO;
import com.yf.zzmusic.pojo.dto.musicCollect.MusicCollectUpdateDTO;
import com.yf.zzmusic.pojo.vo.musicCollect.MusicCollectDetailsVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

import com.yf.zzmusic.service.IMusicCollectService;
import com.yf.zzmusic.pojo.entity.MusicCollect;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 歌单表 前端控制器
 * </p>
 *
 * @author
 * @since 2024-02-26
 */
@RestController
@RequestMapping("/musicCollect")
public class MusicCollectController {
    @Resource
    private IMusicCollectService musicCollectService;

    @PostMapping("/insert")
    public Result save(@Validated @RequestBody MusicCollect musicCollect) {
        boolean result=musicCollectService.save(musicCollect);
        if (result){
            return Result.success("新增成功",null);
        }else{
            return Result.error("新增失败");
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean result= musicCollectService.removeById(id);
        if (result){
            return Result.success("删除成功",null);
        }else{
            return Result.error(Constants.NO_CONTENT,"删除失败");
        }
    }

    @PostMapping("/update")
    public Result update(@Validated @RequestBody MusicCollectUpdateDTO musicCollectUpdateDTO){
        boolean result=musicCollectService.updateMusicCollect(musicCollectUpdateDTO);
        if (result){
            return Result.success("修改成功",null);
        }else{
            return Result.success("修改失败");
        }
    }

    /**
     * 根据歌单id获取歌单详情和歌单中的音乐列表
     */
    @GetMapping("/details")
    public Result detailsMusicList(@Validated MusicCollectDetailsDTO musicCollectDetailsDTO) {
        MusicCollectDetailsVO musicCollectDetailsVO=musicCollectService.musicCollectDetailsByMusicCollectId(musicCollectDetailsDTO);
        if (musicCollectDetailsVO==null){
            return Result.error("查询失败");
        }else {
            return Result.success("查询成功",musicCollectDetailsVO);
        }
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
     MusicCollect musicCollect= musicCollectService.getById(id);
      if (musicCollect!=null){
        return Result.success("查询成功",musicCollect);
      }else{
        return Result.error(Constants.NO_CONTENT,"查询失败");
      }
    }

    @GetMapping("/page")
    public Result findPage(@Validated MusicCollectPageDTO musicCollectPageDTO) {
        PageInfo<MusicCollect> pageInfo=musicCollectService.musicByPage(musicCollectPageDTO);
        if (pageInfo.getTotal()>0) {
            return Result.success("查询成功",pageInfo);
        }else{
            return Result.error(Constants.NO_CONTENT,"列表为空");
        }
    }
}

